"use strict"  // Use strict JavaScript mode

// Pull in the modules we're going to use
var cocos  = require('cocos2d')   // Import the cocos2d module
  , nodes  = cocos.nodes          // Convenient access to 'nodes'
  , events = require('events')    // Import the events module
  , geom    = require('geometry')  // Import the geometry module
  , ccp    = geom.ccp             // Short hand to create points
  ,Texture2D = require('cocos2d/Texture2D').Texture2D
  , title_scene = require('/title_scene')
  
// Convenient access to some constructors
var Layer    = nodes.Layer
  , Scene    = nodes.Scene
  , Label    = nodes.Label
  , Director = cocos.Director
  

function umbc_splash(){
	        umbc_splash.superclass.constructor.call(this)
	
	this.isKeyboardEnabled = true
	this.keysDown = new Array()
	this.scheduleUpdate()
	
		// Add our menu items.
		
		// Fullscreen button....................................................................................................................................
		var item1 = new cocos.nodes.MenuItemImage({ 
					normalImage: "/resources/umbc_gaming.png",
                                        selectedImage: "/resources/umbc_gaming.png",
					callback: this.toTitleCallback.bind(this) })
			
		this.contentSize = item1.contentSize
		item1.anchorPoint = new geom.Point(0,0)
		item1.position = new geom.Point(130,300)	
		
		var menu = new cocos.nodes.Menu({ items: [item1] })
		
		menu.position = geom.ccp(0, 0)

		this.addChild({child: menu})
		
		
		// Animation *******************************************************************************
		var texture = new Texture2D({file: module.dirname + "/resources/animations/flash_bar.png"})

		var frame0 = new cocos.SpriteFrame({texture: texture, rect: new geom.Rect(0, 0, 6, 66)}),
		    frame1 = new cocos.SpriteFrame({texture: texture, rect: new geom.Rect(0, 0, 0, 0)})


		var sprite = new cocos.nodes.Sprite({frame: frame0})
		sprite.position = new geom.Point(500,350)
		this.addChild(sprite)

		var animFrames = [
		    frame0,
		    frame1
		]


		var animation = new cocos.Animation({frames: animFrames, delay: 0.4}),
		    animate   = new cocos.actions.Animate({animation: animation, restoreOriginalFrame: false}),
		    seq       = new cocos.actions.Sequence({actions: [animate,
								 new cocos.actions.FlipX({flipX: true}),
								 animate.copy(),
								 new cocos.actions.FlipX({flipX: false})]});

		sprite.runAction(new cocos.actions.RepeatForever(seq))
		//*******************************************************************************************
								 
}


umbc_splash.inherit(cocos.nodes.Layer, {
    _title: 'No title',
    subtitle: null,
    keysDown: null,

    get title () {
        return this._title
    },

    set title (newTitle) {
        this._title = newTitle
    },

    restartCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = restartAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },

    backCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = backAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },

    nextCallback: function () {
        var director = cocos.Director.sharedDirector

        var scene = new nodes.Scene()
        var action = nextAction()
        scene.addChild({ child: new action() })

        director.replaceScene(scene)
    },
    
    toTitleCallback: function (){
	
        var scene = new Scene()
          , layer = new title_scene()

        // Add our layer to the scene
        scene.addChild(layer)

        // Run the scene
        cocos.Director.sharedDirector.pushScene(scene)
    },
    // Keyboard events
    keyDown: function (evt) {
        var kc = evt.keyCode;
	console.log('down keycode' + kc );
	    
        this.keysDown[kc] = new Boolean(true);
	    
    },
    keyUp: function (evt) {
        var kc = evt.keyCode;
	console.log('up keycode' + kc );
	    
        this.keysDown[kc] = new Boolean(false)
	
    },
    update: function (dt) {
	    
	// Check for 'enter'
	if( this.keysDown[13] == true){
		// Go to the title.
		var scene = new Scene()
		  , layer = new title_scene()

		// Add our layer to the scene
		scene.addChild(layer)

		// Run the scene
		cocos.Director.sharedDirector.pushScene(scene)
	}
	
    },
})

module.exports = umbc_splash